package com.seari.scheduling.plan;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.seari.Utils.DateUtils;
import com.seari.bean.PersonInfoBean;
import com.seari.bean.ReqPlanShiftBean;

@Component
public class PlanMonMidClass implements PlanScheduClass
{
	@Override
	public List<PersonInfoBean> planScheduling(ReqPlanShiftBean bean, List<PersonInfoBean> pesonInfo)
	{
		Map<Integer, PersonInfoBean> per = new HashMap<>();
		List<Integer> onDuty = new ArrayList<>();
		List<PersonInfoBean> persons = new ArrayList<>();

		for (PersonInfoBean person : pesonInfo)
		{
			Integer id = person.getUid();
			onDuty.add(id);
			onDuty.add(id);
			per.put(id, person);
		}

		int days = 0;
		if (bean.getPlanShiftType().equals("week"))
		{
			//
		}
		days = DateUtils.getDaysOfMonth(bean.getReqShiftDate()); // 获取当月天数
		int more = 0; // 早班补足量
		int mire = 2; // 中班补足量

		Map<String, Object> res = new HashMap<>(); // <Integer>
		List<Integer> morningShift = new ArrayList<>(days);
		List<Integer> mightShift = new ArrayList<>(days);

		morningShift = morningShiftScheduling(more, days, onDuty);
		mightShift = nightShiftScheduling(mire, days, onDuty);

		int[] temp = new int[2];
		for (int i = 0; i < morningShift.size(); i++)
		{

			Integer pidMon = morningShift.get(i);
			PersonInfoBean personInfoMon = per.get(pidMon);
			personInfoMon.getShiftInfo().add(i, "早");
			temp[0] = pidMon;

			Integer pidMid = mightShift.get(i);
			PersonInfoBean personInfoMid = per.get(pidMid);
			personInfoMid.getShiftInfo().add(i, "中");
			temp[1] = pidMid;

			Arrays.sort(temp);

			for (int pid : per.keySet())
			{
				if (Arrays.binarySearch(temp, pid) < 0)
				{
					PersonInfoBean personInfo = per.get(pid);
					personInfo.getShiftInfo().add(i, "休");
				}
			}
		}
		
		Iterator<Entry<Integer, PersonInfoBean>> entries = per.entrySet().iterator(); 
		while (entries.hasNext()) { 
		  Entry<Integer, PersonInfoBean> entry = entries.next(); 
		  persons.add(entry.getValue());
		}

		return persons;
	}

	public List<Integer> morningShiftScheduling(int more, int days, List<Integer> onDuty)
	{

		List<Integer> morningShift = new ArrayList<>(days);
		int mormod = (days - more) % 6; // 补足的数量
		int moslice = (days - more) / 6;

		for (int i = more; i >= 1; i--)
		{
			morningShift.add(onDuty.get(onDuty.size() - i));
		}

		for (int i = 0; i < moslice; i++)
		{
			morningShift.addAll(onDuty);
		}

		for (int i = 0; i < mormod; i++)
		{
			morningShift.add(onDuty.get(i));
		}

		more = 6 - mormod;

		return morningShift;
	}

	public List<Integer> nightShiftScheduling(int mire, int days, List<Integer> onDuty)
	{

		List<Integer> mightShift = new ArrayList<>(days);
		int nigmod = (days - mire) % 6;
		int nislice = (days - mire) / 6;

		for (int i = mire; i >= 1; i--)
		{
			mightShift.add(onDuty.get(onDuty.size() - i));
		}

		for (int i = 0; i < nislice; i++)
		{
			mightShift.addAll(onDuty);
		}

		for (int i = 0; i < nigmod; i++)
		{
			mightShift.add(onDuty.get(i));
		}

		mire = 6 - nigmod;

		return mightShift;

	}
}
